package csu.web.jpetstore.web.servlet;

import csu.web.jpetstore.domain.Account;
import csu.web.jpetstore.persistence.impl.AccountDaoImpl;
import csu.web.jpetstore.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Locale;
import javax.servlet.http.HttpServlet;

public class RegisterServlet extends HttpServlet {
    private static final String REGISTER= "/WEB-INF/jsp/account/register.jsp";
    private static final String SIGN_ON_FORM = "/WEB-INF/jsp/account/signOn.jsp";
    private String RegMsg;
    private String username;
    private String userid;
    private String password;
    private String confirm;
    private String firstname;
    private String lastname;
    private String address1;
    private String address2;
    private String city;
    private String status;
    private String state;
    private String email;
    private String phone;
    private String zip;
    private String country;
    private String language;
    private String favcategory;
    private boolean enablemylist;
    private boolean enablebanner;
    private String yzcode;
    private HttpSession session;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        session = req.getSession();
        String code = (String)session.getAttribute("code");

        this.yzcode = req.getParameter("yzcode").toLowerCase(Locale.ROOT);

        this.username=req.getParameter("USERID");
//        this.username=req.getParameter("USERNAME");
        this.password=req.getParameter("PASSWORD");
        this.confirm=req.getParameter("CONFIRM");
        this.firstname=req.getParameter("FIRSTNAME");
        this.lastname=req.getParameter("LASTNAME");
        this.email=req.getParameter("EMAIL");
        this.phone=req.getParameter("PHONE");
        this.address1=req.getParameter("ADDRESS1");
        this.address2=req.getParameter("ADDRESS2");
        this.city=req.getParameter("CITY");
        this.state=req.getParameter("STATE");
        this.zip=req.getParameter("COUNTRY");
        this.language=req.getParameter("LANGUAGE");
        this.favcategory=req.getParameter("PET");
        this.country=req.getParameter("COUNTRY");
        this.enablemylist= Boolean.parseBoolean(req.getParameter("LISTOPTION"));
        this.enablebanner= Boolean.parseBoolean(req.getParameter("BANNEROPTION"));
        System.out.println(firstname);
        System.out.println(userid);
        if(!validate(code)){
            req.setAttribute("RegMsg",RegMsg);
           req.getRequestDispatcher(REGISTER).forward(req,resp);
            return;
        }
        else {
            Account Register=new Account();
            Register.setUsername(this.username);
            Register.setFavouriteCategoryId(this.favcategory);
            Register.setListOption(this.enablemylist);
            Register.setPassword(this.password);
            Register.setBannerOption(this.enablebanner);
            Register.setUsername(this.username);
            Register.setStatus(this.status);
            Register.setLastName(this.lastname);
            Register.setFirstName(this.firstname);
            Register.setPhone(this.phone);
            Register.setState(this.state);
            Register.setZip(this.zip);
            Register.setStatus(this.status);
            Register.setCity(this.city);
            Register.setEmail(this.email);
            Register.setCountry(this.country);
            Register.setAddress1(this.address1);
            Register.setAddress2(this.address2);
            Register.setLanguagePreference(this.language);
//            System.out.println(Register.getFirstName());
//            System.out.println(Register.getUsername());
            AccountDaoImpl accountDAO=new AccountDaoImpl();
            try {
                accountDAO.insertSignon(Register);
                accountDAO.insertProfile(Register);
                accountDAO.insertAccount(Register);
            } catch (Exception e) {
                e.printStackTrace();
            }
//            HttpSession session= req.getSession();
//            session.setAttribute("RegisterAccount",Register);
        }
        req.getRequestDispatcher(SIGN_ON_FORM).forward(req,resp);
    }

    private boolean validate(String code){
        if(!this.password.equals(this.confirm)){
            RegMsg="密码输入不一致！";
            return false;
        }
        else if(username==null||username.equals("")){
            RegMsg = "个人信息不完整！";
            return false;
        }
        else if(address1==null||address1.equals("")){
            if(address2==null||address2.equals("")) {
                RegMsg = "个人信息不完整！";
                return false;
            }
        }
        else if (phone==null||email==null||phone.equals("")||email.equals("")){
            RegMsg = "个人信息不完整！";
            return false;
        }
        else if(!yzcode.equalsIgnoreCase(code)){
            RegMsg = "验证码错误";
            return false;
        }
            return true;
    }
}
